################################
#### load data
################################

c1 <- read.csv("/Users/dk29776/Dropbox/UTAustin/Forecasting/NHSN/data/city_level_data.csv")

c1 <- c1 %>%
  mutate(collection_week = ymd(collection_week),      # Assuming collection_week is in YYYY-MM-DD format
         normalized_influenza = influenza_7_day_sum/(7*(city_2023/100000))) 

mycity = c("HOUSTON", "SAN ANTONIO", "DALLAS", "AUSTIN", "FORT WORTH", "EL PASO", "ARLINGTON",
           "CORPUS CHRISTI", "PLANO", "LAREDO", "LUBBOCK", "IRVING")

Fitting INLA

forecast_date <- as.Date("2023-12-04")

d1 <- c1 %>% 
  filter(state == "TX", city %in% mycity,
         collection_week >= "2021-07-01") %>%
  mutate(city = factor(city, levels = mycity)) %>%
  select(collection_week, state, city, influenza = influenza_7_day_sum)

hyper_epwk <- list(theta=list(prior="loggamma", param=c(1, 0.01))) # more favorable to large jumps
hyper_wk <- list(theta=list(prior="loggamma", param=c(1.5, 0.05))) # precision constrained away from 0; P(prec<1)=0.02%, 


flu_model_shareseason_ar1_dist <-  paste0(
  'count ~ 1 + city + ',
  'f(epiweek, model="rw2", cyclic=TRUE, hyper=hyper_epwk, scale.model=TRUE) + ',
  'f(city_id, model="besagproper", graph=distance_matrix, hyper=hyper_wk, group=t, control.group=list(model="ar1"))' 
)

flu_model_shareseason_ar1 <- flu_model_severalcity(epishare = TRUE, ar=1)

### exchangeable model 

inla_fit_cities(d1 = d1, forecast_date = forecast_date, model = flu_model_shareseason_ar1, 
                mycity = mycity, hyper_epwk, hyper_wk, horizon = 4, 
                joint_season = TRUE, W.model = NULL, dist = FALSE)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

## $summary
## Time used:
##     Pre = 1.5, Running = 2.3, Post = 0.102, Total = 3.89 
## Fixed effects:
##                      mean    sd 0.01quant 0.025quant 0.05quant 0.1quant
## (Intercept)         3.592 0.707     1.910      2.194     2.431    2.696
## citySAN ANTONIO    -1.085 0.931    -3.299     -2.923    -2.611   -2.262
## cityDALLAS         -0.919 0.933    -3.144     -2.767    -2.452   -2.101
## cityAUSTIN         -1.186 0.933    -3.421     -3.040    -2.723   -2.369
## cityFORT WORTH     -1.101 0.932    -3.316     -2.941    -2.629   -2.280
## cityEL PASO        -0.601 0.931    -2.832     -2.451    -2.135   -1.782
## cityARLINGTON      -2.083 0.935    -4.316     -3.936    -3.620   -3.268
## cityCORPUS CHRISTI -1.282 0.933    -3.523     -3.139    -2.821   -2.466
## cityPLANO          -2.159 0.936    -4.399     -4.017    -3.700   -3.345
## cityLAREDO         -4.571 0.972    -6.940     -6.530    -6.190   -5.811
## cityLUBBOCK        -3.578 0.964    -5.921     -5.517    -5.181   -4.807
## cityIRVING         -3.771 0.943    -6.006     -5.629    -5.315   -4.963
##                    0.15quant 0.2quant 0.25quant 0.3quant 0.35quant 0.4quant
## (Intercept)            2.872    3.009     3.126    3.230     3.327    3.418
## citySAN ANTONIO       -2.032   -1.852    -1.698   -1.561    -1.435   -1.315
## cityDALLAS            -1.869   -1.688    -1.534   -1.396    -1.269   -1.149
## cityAUSTIN            -2.136   -1.954    -1.799   -1.661    -1.534   -1.413
## cityFORT WORTH        -2.050   -1.869    -1.715   -1.578    -1.451   -1.331
## cityEL PASO           -1.549   -1.368    -1.213   -1.075    -0.948   -0.828
## cityARLINGTON         -3.035   -2.853    -2.698   -2.560    -2.433   -2.312
## cityCORPUS CHRISTI    -2.232   -2.049    -1.894   -1.756    -1.628   -1.508
## cityPLANO             -3.112   -2.929    -2.774   -2.635    -2.507   -2.387
## cityLAREDO            -5.562   -5.368    -5.204   -5.058    -4.924   -4.798
## cityLUBBOCK           -4.561   -4.369    -4.207   -4.063    -3.930   -3.805
## cityIRVING            -4.731   -4.548    -4.393   -4.254    -4.126   -4.005
##                    0.45quant 0.5quant 0.55quant 0.6quant 0.65quant 0.7quant
## (Intercept)            3.506    3.592     3.679    3.767     3.858    3.954
## citySAN ANTONIO       -1.199   -1.086    -0.972   -0.857    -0.737   -0.610
## cityDALLAS            -1.033   -0.919    -0.805   -0.689    -0.569   -0.442
## cityAUSTIN            -1.297   -1.183    -1.069   -0.954    -0.834   -0.707
## cityFORT WORTH        -1.216   -1.102    -0.988   -0.872    -0.752   -0.626
## cityEL PASO           -0.712   -0.599    -0.485   -0.369    -0.250   -0.123
## cityARLINGTON         -2.196   -2.082    -1.967   -1.851    -1.731   -1.604
## cityCORPUS CHRISTI    -1.392   -1.278    -1.164   -1.048    -0.929   -0.803
## cityPLANO             -2.270   -2.156    -2.042   -1.925    -1.805   -1.678
## cityLAREDO            -4.676   -4.557    -4.438   -4.318    -4.194   -4.064
## cityLUBBOCK           -3.684   -3.566    -3.449   -3.329    -3.206   -3.077
## cityIRVING            -3.888   -3.772    -3.657   -3.540    -3.418   -3.290
##                    0.75quant 0.8quant 0.85quant 0.9quant 0.95quant 0.975quant
## (Intercept)            4.059    4.175     4.313    4.488     4.753      4.989
## citySAN ANTONIO       -0.473   -0.319    -0.138    0.093     0.444      0.757
## cityDALLAS            -0.304   -0.150     0.031    0.262     0.612      0.925
## cityAUSTIN            -0.570   -0.417    -0.237   -0.008     0.340      0.650
## cityFORT WORTH        -0.488   -0.334    -0.152    0.079     0.430      0.744
## cityEL PASO            0.013    0.166     0.346    0.576     0.923      1.233
## cityARLINGTON         -1.467   -1.312    -1.131   -0.900    -0.551     -0.238
## cityCORPUS CHRISTI    -0.666   -0.513    -0.333   -0.105     0.241      0.549
## cityPLANO             -1.541   -1.387    -1.206   -0.975    -0.626     -0.315
## cityLAREDO            -3.923   -3.765    -3.582   -3.349    -2.999     -2.690
## cityLUBBOCK           -2.937   -2.781    -2.598   -2.366    -2.018     -1.709
## cityIRVING            -3.150   -2.994    -2.810   -2.576    -2.220     -1.902
##                    0.99quant   mode kld
## (Intercept)            5.273  3.592   0
## citySAN ANTONIO        1.134 -1.086   0
## cityDALLAS             1.301 -0.919   0
## cityAUSTIN             1.022 -1.183   0
## cityFORT WORTH         1.123 -1.102   0
## cityEL PASO            1.605 -0.599   0
## cityARLINGTON          0.138 -2.082   0
## cityCORPUS CHRISTI     0.920 -1.278   0
## cityPLANO              0.059 -2.156   0
## cityLAREDO            -2.319 -4.557   0
## cityLUBBOCK           -1.339 -3.566   0
## cityIRVING            -1.519 -3.772   0
## 
## Random effects:
##   Name     Model
##     epiweek RW2 model
##    t AR1 model
## 
## Model hyperparameters:
##                        mean    sd 0.01quant 0.025quant 0.05quant 0.1quant
## Precision for epiweek 4.121 3.042     0.840      1.035     1.239    1.530
## Precision for t       0.317 0.049     0.216      0.229     0.241    0.256
## Rho for t             0.911 0.014     0.874      0.881     0.886    0.892
## GroupRho for t        0.132 0.031     0.067      0.076     0.084    0.093
##                       0.15quant 0.2quant 0.25quant 0.3quant 0.35quant 0.4quant
## Precision for epiweek     1.763    1.976     2.183    2.391     2.602    2.820
## Precision for t           0.266    0.274     0.282    0.288     0.295    0.301
## Rho for t                 0.896    0.899     0.902    0.904     0.906    0.908
## GroupRho for t            0.100    0.105     0.110    0.114     0.119    0.123
##                       0.45quant 0.5quant 0.55quant 0.6quant 0.65quant 0.7quant
## Precision for epiweek     3.049    3.298     3.573    3.881     4.229    4.627
## Precision for t           0.307    0.313     0.319    0.326     0.332    0.340
## Rho for t                 0.910    0.912     0.914    0.915     0.917    0.919
## GroupRho for t            0.126    0.130     0.134    0.138     0.142    0.147
##                       0.75quant 0.8quant 0.85quant 0.9quant 0.95quant
## Precision for epiweek     5.104    5.707     6.511    7.682     9.837
## Precision for t           0.348    0.357     0.367    0.381     0.403
## Rho for t                 0.921    0.923     0.925    0.928     0.932
## GroupRho for t            0.152    0.158     0.164    0.173     0.186
##                       0.975quant 0.99quant  mode
## Precision for epiweek     12.216    15.706 2.207
## Precision for t            0.423     0.447 0.307
## Rho for t                  0.936     0.940 0.913
## GroupRho for t             0.198     0.212 0.126
## 
## Deviance Information Criterion (DIC) ...............: 8090.56
## Deviance Information Criterion (DIC, saturated) ....: 2939.82
## Effective number of parameters .....................: 988.59
## 
## Watanabe-Akaike information criterion (WAIC) ...: 8111.94
## Effective number of parameters .................: 742.02
## 
## Marginal log-Likelihood:  -5232.28 
##  is computed 
## Posterior summaries for the linear predictor and the fitted values are computed
## (Posterior marginals needs also 'control.compute=list(return.marginals.predictor=TRUE)')
## 
## 
## $wis
## # A tibble: 48 × 3
##    collection_week city           wis_mean
##    <date>          <fct>             <dbl>
##  1 2023-12-10      HOUSTON         165.   
##  2 2023-12-10      SAN ANTONIO      18.2  
##  3 2023-12-10      DALLAS           88.6  
##  4 2023-12-10      AUSTIN           11.7  
##  5 2023-12-10      FORT WORTH       55.9  
##  6 2023-12-10      EL PASO         105.   
##  7 2023-12-10      ARLINGTON        15.4  
##  8 2023-12-10      CORPUS CHRISTI    3.08 
##  9 2023-12-10      PLANO             7.65 
## 10 2023-12-10      LAREDO            0.425
## # ℹ 38 more rows
### add distance 

inla_fit_cities(d1 = d1, forecast_date = forecast_date, model = flu_model_shareseason_ar1_dist, 
                mycity = mycity, hyper_epwk, hyper_wk, horizon = 4, 
                joint_season = TRUE, W.model = NULL, dist = TRUE)

## $summary
## Time used:
##     Pre = 1.41, Running = 2.46, Post = 0.0821, Total = 3.95 
## Fixed effects:
##                      mean    sd 0.01quant 0.025quant 0.05quant 0.1quant
## (Intercept)         3.586 0.682     1.969      2.240     2.467    2.721
## citySAN ANTONIO    -1.088 0.878    -3.172     -2.821    -2.529   -2.201
## cityDALLAS         -0.923 0.881    -3.019     -2.666    -2.371   -2.041
## cityAUSTIN         -1.184 0.881    -3.288     -2.931    -2.634   -2.302
## cityFORT WORTH     -1.106 0.880    -3.193     -2.842    -2.549   -2.221
## cityEL PASO        -0.596 0.879    -2.695     -2.340    -2.044   -1.712
## cityARLINGTON      -2.072 0.883    -4.173     -3.819    -3.523   -3.192
## cityCORPUS CHRISTI -1.274 0.880    -3.382     -3.023    -2.725   -2.392
## cityPLANO          -2.163 0.884    -4.273     -3.916    -3.618   -3.285
## cityLAREDO         -4.603 0.921    -6.846     -6.459    -6.138   -5.780
## cityLUBBOCK        -3.545 0.911    -5.755     -5.376    -5.060   -4.708
## cityIRVING         -3.807 0.891    -5.917     -5.564    -5.268   -4.936
##                    0.15quant 0.2quant 0.25quant 0.3quant 0.35quant 0.4quant
## (Intercept)            2.890    3.022     3.135    3.236     3.329    3.417
## citySAN ANTONIO       -1.984   -1.813    -1.668   -1.538    -1.419   -1.305
## cityDALLAS            -1.822   -1.650    -1.504   -1.374    -1.254   -1.140
## cityAUSTIN            -2.082   -1.910    -1.764   -1.633    -1.513   -1.399
## cityFORT WORTH        -2.004   -1.833    -1.688   -1.558    -1.438   -1.324
## cityEL PASO           -1.493   -1.321    -1.175   -1.045    -0.925   -0.811
## cityARLINGTON         -2.973   -2.801    -2.655   -2.524    -2.404   -2.290
## cityCORPUS CHRISTI    -2.172   -1.999    -1.853   -1.722    -1.601   -1.487
## cityPLANO             -3.065   -2.892    -2.745   -2.614    -2.493   -2.379
## cityLAREDO            -5.544   -5.360    -5.204   -5.065    -4.938   -4.818
## cityLUBBOCK           -4.476   -4.295    -4.141   -4.004    -3.878   -3.759
## cityIRVING            -4.717   -4.544    -4.397   -4.265    -4.144   -4.029
##                    0.45quant 0.5quant 0.55quant 0.6quant 0.65quant 0.7quant
## (Intercept)            3.502    3.586     3.670    3.755     3.843    3.936
## citySAN ANTONIO       -1.196   -1.088    -0.981   -0.871    -0.758   -0.638
## cityDALLAS            -1.030   -0.922    -0.814   -0.704    -0.591   -0.470
## cityAUSTIN            -1.289   -1.181    -1.073   -0.963    -0.850   -0.730
## cityFORT WORTH        -1.215   -1.107    -0.999   -0.890    -0.776   -0.656
## cityEL PASO           -0.702   -0.594    -0.486   -0.377    -0.263   -0.144
## cityARLINGTON         -2.180   -2.071    -1.963   -1.853    -1.739   -1.619
## cityCORPUS CHRISTI    -1.378   -1.270    -1.162   -1.052    -0.939   -0.820
## cityPLANO             -2.269   -2.160    -2.052   -1.942    -1.828   -1.708
## cityLAREDO            -4.702   -4.589    -4.476   -4.362    -4.244   -4.120
## cityLUBBOCK           -3.645   -3.533    -3.422   -3.308    -3.192   -3.069
## cityIRVING            -3.917   -3.808    -3.699   -3.588    -3.472   -3.351
##                    0.75quant 0.8quant 0.85quant 0.9quant 0.95quant 0.975quant
## (Intercept)            4.037    4.150     4.283    4.452     4.707      4.934
## citySAN ANTONIO       -0.508   -0.363    -0.192    0.026     0.355      0.649
## cityDALLAS            -0.340   -0.195    -0.024    0.194     0.523      0.816
## cityAUSTIN            -0.601   -0.456    -0.286   -0.069     0.258      0.548
## cityFORT WORTH        -0.526   -0.380    -0.209    0.009     0.339      0.633
## cityEL PASO           -0.014    0.130     0.300    0.517     0.843      1.133
## cityARLINGTON         -1.489   -1.343    -1.172   -0.954    -0.624     -0.331
## cityCORPUS CHRISTI    -0.690   -0.546    -0.376   -0.160     0.165      0.454
## cityPLANO             -1.578   -1.432    -1.261   -1.044    -0.716     -0.424
## cityLAREDO            -3.987   -3.838    -3.664   -3.445    -3.116     -2.825
## cityLUBBOCK           -2.936   -2.788    -2.615   -2.397    -2.068     -1.778
## cityIRVING            -3.219   -3.071    -2.898   -2.677    -2.342     -2.045
##                    0.99quant   mode kld
## (Intercept)            5.206  3.586   0
## citySAN ANTONIO        1.001 -1.088   0
## cityDALLAS             1.167 -0.922   0
## cityAUSTIN             0.896 -1.181   0
## cityFORT WORTH         0.987 -1.107   0
## cityEL PASO            1.482 -0.594   0
## cityARLINGTON          0.021 -2.071   0
## cityCORPUS CHRISTI     0.801 -1.270   0
## cityPLANO             -0.074 -2.160   0
## cityLAREDO            -2.480 -4.589   0
## cityLUBBOCK           -1.432 -3.533   0
## cityIRVING            -1.687 -3.808   0
## 
## Random effects:
##   Name     Model
##     epiweek RW2 model
##    city_id Proper version of Besags ICAR model
## 
## Model hyperparameters:
##                        mean    sd 0.01quant 0.025quant 0.05quant 0.1quant
## Precision for epiweek 3.901 1.794     1.373      1.587     1.798    2.080
## Precision for city_id 0.023 0.004     0.015      0.016     0.017    0.018
## Diagonal for city_id  5.053 1.074     3.018      3.263     3.489    3.768
## GroupRho for city_id  0.907 0.014     0.870      0.877     0.883    0.889
##                       0.15quant 0.2quant 0.25quant 0.3quant 0.35quant 0.4quant
## Precision for epiweek     2.294    2.481     2.657    2.827     2.996    3.165
## Precision for city_id     0.019    0.019     0.020    0.021     0.021    0.022
## Diagonal for city_id      3.971    4.138     4.286    4.424     4.558    4.688
## GroupRho for city_id      0.893    0.896     0.898    0.900     0.902    0.904
##                       0.45quant 0.5quant 0.55quant 0.6quant 0.65quant 0.7quant
## Precision for epiweek     3.338    3.520     3.716    3.929     4.164    4.426
## Precision for city_id     0.022    0.023     0.023    0.024     0.024    0.025
## Diagonal for city_id      4.816    4.945     5.076    5.215     5.363    5.524
## GroupRho for city_id      0.906    0.908     0.910    0.911     0.913    0.915
##                       0.75quant 0.8quant 0.85quant 0.9quant 0.95quant
## Precision for epiweek     4.727    5.094     5.564    6.217     7.332
## Precision for city_id     0.026    0.027     0.028    0.029     0.031
## Diagonal for city_id      5.701    5.904     6.151    6.478     6.993
## GroupRho for city_id      0.917    0.919     0.922    0.925     0.929
##                       0.975quant 0.99quant  mode
## Precision for epiweek      8.475    10.019 2.881
## Precision for city_id      0.033     0.035 0.022
## Diagonal for city_id       7.471     8.073 4.736
## GroupRho for city_id       0.932     0.936 0.909
## 
## Deviance Information Criterion (DIC) ...............: 8120.05
## Deviance Information Criterion (DIC, saturated) ....: 2969.30
## Effective number of parameters .....................: 995.06
## 
## Watanabe-Akaike information criterion (WAIC) ...: 8143.54
## Effective number of parameters .................: 748.11
## 
## Marginal log-Likelihood:  -5234.76 
##  is computed 
## Posterior summaries for the linear predictor and the fitted values are computed
## (Posterior marginals needs also 'control.compute=list(return.marginals.predictor=TRUE)')
## 
## 
## $wis
## # A tibble: 48 × 3
##    collection_week city           wis_mean
##    <date>          <fct>             <dbl>
##  1 2023-12-10      HOUSTON         167.   
##  2 2023-12-10      SAN ANTONIO      18.3  
##  3 2023-12-10      DALLAS           90.3  
##  4 2023-12-10      AUSTIN           11.7  
##  5 2023-12-10      FORT WORTH       56.6  
##  6 2023-12-10      EL PASO         105.   
##  7 2023-12-10      ARLINGTON        15.6  
##  8 2023-12-10      CORPUS CHRISTI    3.12 
##  9 2023-12-10      PLANO             7.70 
## 10 2023-12-10      LAREDO            0.434
## # ℹ 38 more rows
################################
#### fitting INLA with several dates
################################

### exchangeable model 
inla_fit_several_forecastdate_cities(d1, forecast_date = c("2023-10-01", "2023-11-19", "2024-01-07", "2024-02-18", "2024-03-24"), 
                                     model = flu_model_shareseason_ar1, mycity = mycity, hyper_epwk = hyper_epwk, hyper_wk = hyper_wk, horizon = 4,
                                     joint_season = TRUE, W.model = "ar1, share seaseon, exchangeable")

## $summary
## Time used:
##     Pre = 1.35, Running = 2.21, Post = 0.0924, Total = 3.65 
## Fixed effects:
##                      mean    sd 0.01quant 0.025quant 0.05quant 0.1quant
## (Intercept)         3.703 0.702     2.019      2.309     2.548    2.815
## citySAN ANTONIO    -1.029 0.916    -3.211     -2.837    -2.528   -2.184
## cityDALLAS         -0.760 0.918    -2.954     -2.577    -2.266   -1.919
## cityAUSTIN         -1.132 0.918    -3.333     -2.953    -2.640   -2.292
## cityFORT WORTH     -0.836 0.917    -3.014     -2.642    -2.335   -1.992
## cityEL PASO        -0.603 0.917    -2.802     -2.422    -2.109   -1.761
## cityARLINGTON      -1.896 0.920    -4.092     -3.715    -3.403   -3.057
## cityCORPUS CHRISTI -1.392 0.918    -3.593     -3.213    -2.899   -2.551
## cityPLANO          -1.924 0.921    -4.124     -3.746    -3.433   -3.086
## cityLAREDO         -3.950 0.943    -6.226     -5.831    -5.506   -5.145
## cityLUBBOCK        -3.035 0.940    -5.309     -4.914    -4.588   -4.227
## cityIRVING         -3.585 0.925    -5.786     -5.410    -5.099   -4.752
##                    0.15quant 0.2quant 0.25quant 0.3quant 0.35quant 0.4quant
## (Intercept)            2.990    3.127     3.243    3.347     3.442    3.532
## citySAN ANTONIO       -1.958   -1.781    -1.631   -1.496    -1.372   -1.255
## cityDALLAS            -1.692   -1.514    -1.363   -1.228    -1.103   -0.986
## cityAUSTIN            -2.064   -1.885    -1.733   -1.598    -1.474   -1.356
## cityFORT WORTH        -1.767   -1.590    -1.440   -1.306    -1.182   -1.064
## cityEL PASO           -1.533   -1.354    -1.203   -1.068    -0.943   -0.826
## cityARLINGTON         -2.829   -2.651    -2.499   -2.364    -2.240   -2.122
## cityCORPUS CHRISTI    -2.323   -2.144    -1.992   -1.857    -1.733   -1.615
## cityPLANO             -2.858   -2.679    -2.528   -2.392    -2.268   -2.150
## cityLAREDO            -4.908   -4.723    -4.566   -4.426    -4.298   -4.176
## cityLUBBOCK           -3.990   -3.805    -3.648   -3.509    -3.381   -3.260
## cityIRVING            -4.524   -4.345    -4.193   -4.057    -3.932   -3.814
##                    0.45quant 0.5quant 0.55quant 0.6quant 0.65quant 0.7quant
## (Intercept)            3.619    3.705     3.790    3.877     3.967    4.062
## citySAN ANTONIO       -1.142   -1.031    -0.919   -0.806    -0.688   -0.564
## cityDALLAS            -0.872   -0.760    -0.649   -0.535    -0.417   -0.293
## cityAUSTIN            -1.242   -1.131    -1.019   -0.906    -0.788   -0.664
## cityFORT WORTH        -0.951   -0.840    -0.728   -0.615    -0.497   -0.372
## cityEL PASO           -0.712   -0.601    -0.489   -0.376    -0.259   -0.135
## cityARLINGTON         -2.008   -1.896    -1.784   -1.670    -1.552   -1.428
## cityCORPUS CHRISTI    -1.501   -1.390    -1.278   -1.165    -1.048   -0.924
## cityPLANO             -2.036   -1.924    -1.812   -1.698    -1.580   -1.455
## cityLAREDO            -4.059   -3.945    -3.830   -3.714    -3.593   -3.466
## cityLUBBOCK           -3.143   -3.029    -2.914   -2.798    -2.678   -2.552
## cityIRVING            -3.699   -3.587    -3.474   -3.359    -3.241   -3.115
##                    0.75quant 0.8quant 0.85quant 0.9quant 0.95quant 0.975quant
## (Intercept)            4.165    4.280     4.415    4.588     4.850      5.084
## citySAN ANTONIO       -0.429   -0.277    -0.099    0.129     0.477      0.790
## cityDALLAS            -0.158   -0.007     0.172    0.400     0.746      1.058
## cityAUSTIN            -0.529   -0.378    -0.201    0.026     0.371      0.681
## cityFORT WORTH        -0.237   -0.085     0.095    0.324     0.674      0.989
## cityEL PASO            0.000    0.150     0.327    0.554     0.898      1.207
## cityARLINGTON         -1.292   -1.141    -0.962   -0.734    -0.386     -0.073
## cityCORPUS CHRISTI    -0.789   -0.638    -0.461   -0.234     0.110      0.419
## cityPLANO             -1.320   -1.168    -0.990   -0.761    -0.414     -0.101
## cityLAREDO            -3.329   -3.175    -2.994   -2.763    -2.414     -2.102
## cityLUBBOCK           -2.415   -2.262    -2.082   -1.852    -1.505     -1.195
## cityIRVING            -2.978   -2.825    -2.645   -2.414    -2.063     -1.747
##                    0.99quant   mode kld
## (Intercept)            5.368  3.705   0
## citySAN ANTONIO        1.170 -1.030   0
## cityDALLAS             1.436 -0.760   0
## cityAUSTIN             1.057 -1.131   0
## cityFORT WORTH         1.372 -0.840   0
## cityEL PASO            1.582 -0.601   0
## cityARLINGTON          0.306 -1.896   0
## cityCORPUS CHRISTI     0.794 -1.390   0
## cityPLANO              0.277 -1.924   0
## cityLAREDO            -1.724 -3.945   0
## cityLUBBOCK           -0.819 -3.029   0
## cityIRVING            -1.364 -3.587   0
## 
## Random effects:
##   Name     Model
##     epiweek RW2 model
##    t AR1 model
## 
## Model hyperparameters:
##                        mean    sd 0.01quant 0.025quant 0.05quant 0.1quant
## Precision for epiweek 3.214 2.181     0.645      0.806     0.975    1.216
## Precision for t       0.345 0.056     0.229      0.244     0.258    0.275
## Rho for t             0.928 0.012     0.896      0.902     0.907    0.912
## GroupRho for t        0.149 0.028     0.089      0.097     0.105    0.114
##                       0.15quant 0.2quant 0.25quant 0.3quant 0.35quant 0.4quant
## Precision for epiweek     1.412    1.590     1.759    1.927     2.100    2.277
## Precision for t           0.287    0.297     0.305    0.313     0.320    0.328
## Rho for t                 0.915    0.918     0.920    0.922     0.924    0.925
## GroupRho for t            0.120    0.125     0.129    0.133     0.137    0.141
##                       0.45quant 0.5quant 0.55quant 0.6quant 0.65quant 0.7quant
## Precision for epiweek     2.462    2.657     2.868    3.103     3.369    3.673
## Precision for t           0.335    0.342     0.349    0.356     0.364    0.372
## Rho for t                 0.927    0.928     0.930    0.931     0.933    0.935
## GroupRho for t            0.144    0.148     0.151    0.155     0.159    0.163
##                       0.75quant 0.8quant 0.85quant 0.9quant 0.95quant
## Precision for epiweek     4.028    4.463     5.036    5.869     7.352
## Precision for t           0.381    0.391     0.403    0.419     0.443
## Rho for t                 0.936    0.938     0.940    0.943     0.946
## GroupRho for t            0.167    0.172     0.178    0.186     0.197
##                       0.975quant 0.99quant  mode
## Precision for epiweek      8.944    11.240 1.827
## Precision for t            0.465     0.491 0.337
## Rho for t                  0.949     0.953 0.929
## GroupRho for t             0.207     0.219 0.146
## 
## Deviance Information Criterion (DIC) ...............: 9729.35
## Deviance Information Criterion (DIC, saturated) ....: 3387.62
## Effective number of parameters .....................: 1108.42
## 
## Watanabe-Akaike information criterion (WAIC) ...: 9758.02
## Effective number of parameters .................: 840.75
## 
## Marginal log-Likelihood:  -6223.80 
##  is computed 
## Posterior summaries for the linear predictor and the fitted values are computed
## (Posterior marginals needs also 'control.compute=list(return.marginals.predictor=TRUE)')
## 
## 
## $wis
## # A tibble: 240 × 3
##    collection_week city           wis_mean
##    <date>          <fct>             <dbl>
##  1 2023-10-01      HOUSTON          13.9  
##  2 2023-10-01      SAN ANTONIO      12.5  
##  3 2023-10-01      DALLAS            7.25 
##  4 2023-10-01      AUSTIN           10.3  
##  5 2023-10-01      FORT WORTH        7.53 
##  6 2023-10-01      EL PASO           8.94 
##  7 2023-10-01      ARLINGTON         8.42 
##  8 2023-10-01      CORPUS CHRISTI    2.66 
##  9 2023-10-01      PLANO             1.30 
## 10 2023-10-01      LAREDO            0.394
## # ℹ 230 more rows
### add distance 
inla_fit_several_forecastdate_cities(d1, forecast_date = c("2023-10-01", "2023-11-19", "2024-01-07", "2024-02-18", "2024-03-24"), 
                                     model = flu_model_shareseason_ar1_dist, mycity = mycity, hyper_epwk = hyper_epwk, hyper_wk = hyper_wk, horizon = 4,
                                     joint_season = TRUE, W.model = "ar1, share seaseon, dist", dist = TRUE)

## $summary
## Time used:
##     Pre = 1.68, Running = 2.76, Post = 0.0952, Total = 4.54 
## Fixed effects:
##                      mean    sd 0.01quant 0.025quant 0.05quant 0.1quant
## (Intercept)         3.698 0.685     2.058      2.337     2.569    2.829
## citySAN ANTONIO    -1.033 0.878    -3.118     -2.764    -2.470   -2.142
## cityDALLAS         -0.766 0.880    -2.861     -2.505    -2.209   -1.878
## cityAUSTIN         -1.135 0.880    -3.236     -2.877    -2.580   -2.248
## cityFORT WORTH     -0.844 0.879    -2.926     -2.574    -2.281   -1.954
## cityEL PASO        -0.602 0.878    -2.702     -2.343    -2.046   -1.714
## cityARLINGTON      -1.890 0.881    -3.989     -3.632    -3.336   -3.005
## cityCORPUS CHRISTI -1.391 0.879    -3.493     -3.134    -2.836   -2.504
## cityPLANO          -1.932 0.882    -4.034     -3.676    -3.379   -3.048
## cityLAREDO         -3.986 0.904    -6.167     -5.792    -5.481   -5.135
## cityLUBBOCK        -3.026 0.901    -5.198     -4.825    -4.516   -4.170
## cityIRVING         -3.609 0.887    -5.715     -5.358    -5.062   -4.730
##                    0.15quant 0.2quant 0.25quant 0.3quant 0.35quant 0.4quant
## (Intercept)            3.000    3.134     3.247    3.349     3.442    3.531
## citySAN ANTONIO       -1.925   -1.756    -1.611   -1.482    -1.363   -1.250
## cityDALLAS            -1.660   -1.490    -1.344   -1.215    -1.095   -0.982
## cityAUSTIN            -2.029   -1.858    -1.712   -1.583    -1.463   -1.350
## cityFORT WORTH        -1.738   -1.568    -1.424   -1.295    -1.176   -1.063
## cityEL PASO           -1.495   -1.324    -1.179   -1.049    -0.930   -0.817
## cityARLINGTON         -2.787   -2.616    -2.470   -2.341    -2.221   -2.108
## cityCORPUS CHRISTI    -2.285   -2.114    -1.968   -1.839    -1.719   -1.606
## cityPLANO             -2.829   -2.658    -2.512   -2.382    -2.262   -2.149
## cityLAREDO            -4.907   -4.729    -4.578   -4.443    -4.320   -4.203
## cityLUBBOCK           -3.944   -3.766    -3.616   -3.482    -3.358   -3.242
## cityIRVING            -4.511   -4.340    -4.193   -4.063    -3.942   -3.828
##                    0.45quant 0.5quant 0.55quant 0.6quant 0.65quant 0.7quant
## (Intercept)            3.616    3.700     3.783    3.868     3.957    4.049
## citySAN ANTONIO       -1.142   -1.035    -0.927   -0.818    -0.705   -0.586
## cityDALLAS            -0.873   -0.766    -0.658   -0.549    -0.436   -0.317
## cityAUSTIN            -1.241   -1.133    -1.026   -0.917    -0.804   -0.685
## cityFORT WORTH        -0.954   -0.847    -0.740   -0.631    -0.517   -0.398
## cityEL PASO           -0.708   -0.601    -0.493   -0.385    -0.272   -0.153
## cityARLINGTON         -1.998   -1.891    -1.783   -1.674    -1.560   -1.441
## cityCORPUS CHRISTI    -1.497   -1.390    -1.282   -1.173    -1.061   -0.941
## cityPLANO             -2.039   -1.932    -1.824   -1.715    -1.601   -1.481
## cityLAREDO            -4.090   -3.979    -3.869   -3.757    -3.641   -3.519
## cityLUBBOCK           -3.129   -3.019    -2.909   -2.798    -2.682   -2.561
## cityIRVING            -3.718   -3.610    -3.502   -3.392    -3.277   -3.157
##                    0.75quant 0.8quant 0.85quant 0.9quant 0.95quant 0.975quant
## (Intercept)            4.150    4.263     4.395    4.564     4.819      5.047
## citySAN ANTONIO       -0.456   -0.311    -0.140    0.078     0.410      0.707
## cityDALLAS            -0.187   -0.042     0.129    0.347     0.678      0.975
## cityAUSTIN            -0.555   -0.411    -0.240   -0.023     0.307      0.601
## cityFORT WORTH        -0.268   -0.122     0.050    0.269     0.603      0.902
## cityEL PASO           -0.024    0.121     0.291    0.508     0.836      1.130
## cityARLINGTON         -1.311   -1.165    -0.994   -0.775    -0.443     -0.146
## cityCORPUS CHRISTI    -0.812   -0.667    -0.497   -0.280     0.048      0.342
## cityPLANO             -1.351   -1.206    -1.035   -0.816    -0.485     -0.188
## cityLAREDO            -3.387   -3.239    -3.066   -2.846    -2.513     -2.217
## cityLUBBOCK           -2.429   -2.282    -2.109   -1.890    -1.558     -1.263
## cityIRVING            -3.026   -2.879    -2.706   -2.485    -2.151     -1.851
##                    0.99quant   mode kld
## (Intercept)            5.321  3.700   0
## citySAN ANTONIO        1.066 -1.034   0
## cityDALLAS             1.332 -0.766   0
## cityAUSTIN             0.956 -1.133   0
## cityFORT WORTH         1.262 -0.847   0
## cityEL PASO            1.484 -0.601   0
## cityARLINGTON          0.211 -1.891   0
## cityCORPUS CHRISTI     0.696 -1.390   0
## cityPLANO              0.169 -1.932   0
## cityLAREDO            -1.862 -3.980   0
## cityLUBBOCK           -0.907 -3.019   0
## cityIRVING            -1.490 -3.610   0
## 
## Random effects:
##   Name     Model
##     epiweek RW2 model
##    city_id Proper version of Besags ICAR model
## 
## Model hyperparameters:
##                        mean    sd 0.01quant 0.025quant 0.05quant 0.1quant
## Precision for epiweek 3.788 3.375     0.576      0.735     0.906    1.158
## Precision for city_id 0.026 0.005     0.016      0.018     0.019    0.020
## Diagonal for city_id  4.657 0.884     2.961      3.171     3.362    3.597
## GroupRho for city_id  0.924 0.012     0.894      0.899     0.904    0.909
##                       0.15quant 0.2quant 0.25quant 0.3quant 0.35quant 0.4quant
## Precision for epiweek     1.366    1.559     1.750    1.945     2.145    2.355
## Precision for city_id     0.021    0.022     0.023    0.023     0.024    0.024
## Diagonal for city_id      3.766    3.905     4.028    4.143     4.253    4.360
## GroupRho for city_id      0.912    0.915     0.917    0.919     0.920    0.922
##                       0.45quant 0.5quant 0.55quant 0.6quant 0.65quant 0.7quant
## Precision for epiweek     2.578    2.823     3.097    3.407     3.762    4.175
## Precision for city_id     0.025    0.026     0.026    0.027     0.027    0.028
## Diagonal for city_id      4.466    4.571     4.680    4.795     4.917    5.050
## GroupRho for city_id      0.923    0.925     0.926    0.928     0.929    0.931
##                       0.75quant 0.8quant 0.85quant 0.9quant 0.95quant
## Precision for epiweek     4.674    5.315     6.187    7.486     9.949
## Precision for city_id     0.029    0.030     0.031    0.032     0.034
## Diagonal for city_id      5.195    5.361     5.564    5.832     6.250
## GroupRho for city_id      0.933    0.934     0.937    0.939     0.943
##                       0.975quant 0.99quant  mode
## Precision for epiweek     12.767    17.043 1.676
## Precision for city_id      0.036     0.038 0.025
## Diagonal for city_id       6.639     7.122 4.399
## GroupRho for city_id       0.946     0.949 0.926
## 
## Deviance Information Criterion (DIC) ...............: 9754.45
## Deviance Information Criterion (DIC, saturated) ....: 3412.72
## Effective number of parameters .....................: 1114.26
## 
## Watanabe-Akaike information criterion (WAIC) ...: 9782.25
## Effective number of parameters .................: 844.41
## 
## Marginal log-Likelihood:  -6225.40 
##  is computed 
## Posterior summaries for the linear predictor and the fitted values are computed
## (Posterior marginals needs also 'control.compute=list(return.marginals.predictor=TRUE)')
## 
## 
## $wis
## # A tibble: 240 × 3
##    collection_week city           wis_mean
##    <date>          <fct>             <dbl>
##  1 2023-10-01      HOUSTON          14.2  
##  2 2023-10-01      SAN ANTONIO      12.4  
##  3 2023-10-01      DALLAS            7.04 
##  4 2023-10-01      AUSTIN           10.3  
##  5 2023-10-01      FORT WORTH        7.64 
##  6 2023-10-01      EL PASO           8.75 
##  7 2023-10-01      ARLINGTON         8.20 
##  8 2023-10-01      CORPUS CHRISTI    2.69 
##  9 2023-10-01      PLANO             1.35 
## 10 2023-10-01      LAREDO            0.426
## # ℹ 230 more rows